import com.dmotiko.general.*
import com.dmotiko.lib.EventDispatcherPro;
//import mx.events.EventDispatcher;
import mx.utils.Delegate;

class com.dmotiko.general.WebSite {
	
	public static var SECTION_CHANGED:String = "website_sectionchanged";
	public static var LANGUAGE_CHANGED:String = "website_languagechanged";
	public static var SOUND_CHANGED:String = "website_soundchanged";
	public static var DATA_CHANGED:String = "website_datachanged";
	
	private static var app:WebSite;
		
	public var addEventListener:Function;
	public var removeEventListener:Function;
	public var dispatchEvent:Function;
	
	private var activeSection:WebSiteSection;
	private var sSection:String;
	private var sLanguage:String;
	private var data:Object;
	private var timeline:MovieClip;
	
	private var txtConsole:TextField;
	private var bSound:Boolean;
			
	public static function init(timeline:MovieClip){
		if(!app) app = new WebSite(timeline);
	}
	
	public static function getApp():WebSite {
		return app;
	}
	
	private function WebSite(timeline:MovieClip){
		EventDispatcherPro.initialize(this);
		this.timeline = timeline;
		var root:WebSite = this;
		if ( _root.CONSOLE ) {
			timeline.createTextField( "txtConsole", 1000, 0, Stage.height - 20, Stage.width, 30 );
			this.txtConsole = timeline.txtConsole;
			this.txtConsole.multiline = true;
			this.txtConsole.background = true;
			this.txtConsole.backgroundColor = 0xFFFFFF;
			this.txtConsole.borderColor = 0;
			this.log("Console Initialized");
		} else {
			this.log("WebSite | Console Disabled");
		}
	}
		
	/**
	 * @author= sminutoli
	 * @usage= For override purposes
	 */
	private function initSite():Void {
		return;
	}
	
	public function getSound():Boolean {
		return bSound;
	}
	public function setSound(bSound:Boolean):Void {
		this.bSound = bSound;
		dispatchEvent( { type: SOUND_CHANGED, target: this, data: bSound } );
	}
	
	public function getSection():String {
		return this.sSection;
	}
	/**
	 * 
	 * @param	sSection
	 * @return	devuelve true si se pudo cambiar
	 */
	public function setSection(sSection:String):Boolean {
		var last:String = this.sSection;
		this.sSection = sSection;
		dispatchEvent( { target: this, type: WebSite.SECTION_CHANGED, last: last, section: sSection } );
		if ( _root.analytics ) {
			getURL(_root.analytics+"('"+sSection+"')");
		}
		return true;
	}
	
	public function getLanguage():String {
		return this.sLanguage;
	}
	public function setLanguage(sLanguage:String):Void {
		//log("WebSite | setLanguage= " + sLanguage);
		this.sLanguage = sLanguage;
		dispatchEvent( { target: this, type: WebSite.LANGUAGE_CHANGED, value: sLanguage } );
	}
	
	public function getData():Object{
		return this.data;
	}
	public function setData(data:Object):Void {
		this.data = data;
		dispatchEvent( { target: this, type: WebSite.DATA_CHANGED } );
	}
	public function log(msg:String):Void {
		
		if ( _root.CONSOLE) {
			if (txtConsole.nLine == undefined) txtConsole.nLine = 1;
			txtConsole.text += txtConsole.nLine + " | " + msg;
			txtConsole.text += "\n";
			txtConsole.scroll = txtConsole.maxscroll;
			txtConsole.nLine++;
		} else if ( _root.FIREBUG ) {
			getURL("javascript:console.log('"+msg+"');");
			
		} else {
			trace(msg);
		}
	}
	
	public function getActiveSection():WebSiteSection { 
		return activeSection; 
	}
	
	public function setActiveSection(value:WebSiteSection):Void {
		activeSection = value;
	}
	
}